package com.loe.mvp

import android.app.Activity
import android.app.AlertDialog
import android.content.DialogInterface
import androidx.annotation.StyleRes
import android.view.Gravity
import android.view.ViewGroup
import android.view.WindowManager
import com.loe.mvp.ext_app.setOnBackListener
import com.loe.mvp.ext_java.safe
import com.loe.mvp.initer.BaseLoading
import com.loe.mvp.initer.BaseToast

/**
 * Dialog基类
 */
abstract class BaseDialog(protected val activity: Activity, private val resId: Int) : BaseView
{
    protected val dialog: AlertDialog = AlertDialog.Builder(activity).create()
    private var mToast: BaseToast = BaseToast(activity)
    private var mLoading: BaseLoading = BaseLoading(activity)

    open var width = ViewGroup.LayoutParams.MATCH_PARENT
    open var height = ViewGroup.LayoutParams.WRAP_CONTENT

    var cancelable: Boolean = true
        set(value) = dialog.setCancelable(value)

    var canceledOnTouchOutside: Boolean = true
        set(value) = dialog.setCanceledOnTouchOutside(value)

    private var onConfirm: ((DialogInterface) -> Unit)? = null

    lateinit var rootView: ViewGroup

    abstract fun ViewGroup.onView()

    val window get() = dialog.window

    @JvmOverloads
    fun show(gravity: Int = Gravity.CENTER)
    {
        safe()
        {
            if (!dialog.isShowing)
            {
                dialog.show()
                if (window != null)
                {
                    window?.setContentView(resId)
                    window?.setBackgroundDrawableResource(android.R.color.transparent)
                    window?.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM)
                    window?.setLayout(width, height)
                    window?.setGravity(gravity)
                    rootView = window?.decorView as ViewGroup
                    rootView.setPadding(0, 0, 0, 0)
                    rootView.onView()
                }
            }
        }
    }

    fun cancel()
    {
        dialog.cancel()
    }

    fun dismiss()
    {
        dialog.dismiss()
    }

    override val root: Activity get() = activity
    override val toast: BaseToast get() = mToast
    override val loading: BaseLoading get() = mLoading

    val isShowing: Boolean get() = dialog.isShowing

    /** 设置弹框动画 */
    fun setAnimation(@StyleRes resId: Int = R.style.dialogAnimation)
    {
        window?.setWindowAnimations(resId)
    }

    /** 设置遮罩透明度 */
    fun setDimAmount(amount: Float = 0.4f)
    {
        window?.setDimAmount(amount)
    }

    fun setCancelAfterDismiss()
    {
        dialog.setOnCancelListener()
        {
            dismiss()
        }
    }

    fun setOnDismissListener(on: (DialogInterface) -> Unit): BaseDialog
    {
        dialog.setOnDismissListener(on)
        return this
    }

    fun setOnCancelListener(on: (DialogInterface) -> Unit): BaseDialog
    {
        dialog.setOnCancelListener(on)
        return this
    }

    fun setOnConfirmListener(on: (DialogInterface) -> Unit): BaseDialog
    {
        onConfirm = on
        return this
    }

    fun confirm()
    {
        onConfirm?.invoke(dialog)
    }

    companion object
    {
        @JvmStatic
        fun create(activity: Activity, resId: Int, onView: ViewGroup.() -> Unit): BaseDialog
        {
            return object : BaseDialog(activity, resId)
            {
                override fun ViewGroup.onView()
                {
                    onView()
                }
            }
        }
    }
}